# Figure B.2: Individual Subjective Salience


# 1. Load Packages ----

library(tidyverse)
library(ggpubr)
library(haven)

# 2. Read in Data ----

load(file = "df_voxit_individual_allrefs.RData")

# 3. Data Preparation ----
# Generate Average Salience by Legal Form
sal_prop_type <- voxit_complete_salience %>%
  mutate(typex = as.numeric(haven::as_factor(typex))) %>%
  group_by(typex) %>%
  summarise(se = sd(salience, na.rm = T)/sqrt(n()),
            salience = mean(salience, na.rm = T)) %>%
  mutate(ci_95 = se*1.96)

# Relationship between salience and turnout
abstimmungen <- voxit_complete_salience %>%
  group_by(datum_merge) %>%
  summarise(turnout = mean(turnout, na.rm = T)*100,
            salience = mean(salience, na.rm = T),
            turnout_admin = mean(turnout_admin, na.rm = T),
            difficulty = mean(difficulty),
            nobs=sum(!is.na(turnout)))


# 4. Create Graph ----
# Distribution Salience
dist_sal <- ggplot(data = voxit_complete_salience) +
  geom_histogram(mapping = aes(x = salience, y = (..count..)/sum(..count..)),
                 fill = "grey", color = "black", binwidth = 1) +
  scale_x_continuous(name = "Salience",
                     breaks = seq(0,10,1)) +
  scale_y_continuous(name = "Relative frequency",
                     breaks = seq(0,0.2,0.05),
                     labels = scales::percent_format(accuracy = 5L),
                     limits = c(0,0.2)) +
  theme_bw(base_size = 9) +
  ggtitle(label = "Distribution of Salience") +
  theme(plot.title = element_text(size = 12))

# Salience by legal form
sal_prop <- ggplot(data = sal_prop_type, 
                   mapping = aes(x = factor(typex, levels = c(3,2,1,4)),
                                                       y = salience)) +
  geom_point() +
  geom_errorbar(mapping = aes(ymin = salience-ci_95, ymax = salience+ci_95),
                width = 0.2) +
  #  geom_text(mapping = aes(label = round(salience, digits = 1)), vjust = -0.4) +
  scale_y_continuous(name = "Average salience",
                     breaks = seq(4,6,0.5),
                     labels = seq(4,6,0.5),
                     limits = c(4,6)) +
  scale_x_discrete(name = "",
                   breaks = c(3,2,1,4),
                   labels = c("Compulsory \n referendum", "Optional \n referendum",
                              "Popular \n initiative", "Counter \n proposition")) +
  theme_bw(base_size = 9) +
  ggtitle(label = "Salience by Legal Form") +
  theme(plot.title = element_text(size = 12))

# Self-Reported Turnout and Salience
ind <- ggplot(data = abstimmungen, 
              mapping = aes(x = salience, y = turnout)) +
  geom_point() +
  theme_bw(base_size = 9) +
  ylab("Average turnout") +
  scale_y_continuous(limits = c(20,100),
                     breaks = seq(20,100,10),
                     labels = c("20%", "30%", "40%", 
                                "50%", "60%", "70%", 
                                "80%", "90%", "100%")) +
  scale_x_continuous(limits = c(2,8),
                     breaks = seq(2,8,1),
                     labels = seq(2,8,1)) +
  xlab("Average salience") +
  ggtitle("Self-Reported Turnout and Salience")  +
  theme(plot.title = element_text(size = 12))

# Admin Turnout and Salience
admin <- ggplot(data = abstimmungen, 
                mapping = aes(x = salience, y = turnout_admin)) +
  geom_point() +
  theme_bw(base_size = 9) +
  ylab("Average turnout") +
  scale_y_continuous(limits = c(20,100),
                     breaks = seq(20,100,10),
                     labels = c("20%", "30%", "40%", 
                                "50%", "60%", "70%", 
                                "80%", "90%", "100%")) +
  scale_x_continuous(limits = c(2,8),
                     breaks = seq(2,8,1),
                     labels = seq(2,8,1)) +
  xlab("Average salience") +
  ggtitle("Administrative Turnout and Salience")  +
  theme(plot.title = element_text(size = 12))

# 5. Save Graph ----
# Final Graph
ggarrange(dist_sal, sal_prop, ind, admin, ncol = 2, nrow = 2)
ggsave(filename = "FigureB2.pdf", width = 9.35, height = 5.58)

